package com.chinatgl.latte.ec.main.index;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.chinatgl.latte.adapter.TabFragmentAdapter;
import com.chinatgl.latte.delegates.BaseFragment;
import com.chinatgl.latte.delegates.bottom.BottomItemDelegate;
import com.chinatgl.latte.ec.API;
import com.chinatgl.latte.ec.R;
import com.chinatgl.latte.ec.main.index._1.MYHomeFragment;
import com.chinatgl.latte.ec.params.MapUtil;
import com.chinatgl.latte.ec.mode.CategoryBean;
import com.chinatgl.latte.net.NetRequest;
import com.chinatgl.latte.net.RestCreator;
import com.chinatgl.latte.net.listener.onNetRquestListener;
import com.chinatgl.latte.util.json.JsonUtil;
import com.chinatgl.latte.util.json.Result;
import com.google.gson.reflect.TypeToken;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.WeakHashMap;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 文件描述: 首界面
 * 作者: Created by 杨伟乔 on 2017/8/9 0009.
 * 版本号: 1.0
 */

public class IndexDelegate extends BottomItemDelegate {

    private TabFragmentAdapter tabFragmentAdapter;
    private CommonNavigatorAdapter commonNavigatorAdapter;
    private ViewPager pager;
    private MagicIndicator magicIndicator;

    @Override
    public CharSequence getTitle() {
        return "首页";
    }

    @Override
    public Object getContentView() {
        return R.layout.fragment_home;
    }


    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    private ArrayList<BaseFragment> baseFragments;

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);


        WeakHashMap<String, Object> map = MapUtil.getParams();
        Type type = new TypeToken<Result<ArrayList<CategoryBean>>>() {
        }.getType();
        //获取顶级的分类
        NetRequest.post(API.TOPCATLIST, map, type, true, 60 * 24, new onNetRquestListener() {
            @Override
            public void requestdataFinish(String catalog, Object bean) {

                Result<ArrayList<CategoryBean>> result = (Result<ArrayList<CategoryBean>>) bean;
                if (result.getCode() == 0) {
                    ArrayList<CategoryBean> result1 = result.getResult();

                    //根据返回的数据创建对应的子界面
                    baseFragments.clear();
                    //创建子界面的class 添加首界面
                    baseFragments.add(new MYHomeFragment());
                    //添加其他的界面
                    for (int i = 0; i < result1.size(); i++) {
                        baseFragments.add(new MYHomeFragment());
                    }
                    stateLayout.showContentView();
                    tabFragmentAdapter.notifyDataSetChanged();
                    commonNavigatorAdapter.notifyDataSetChanged();

                } else {
                    //失败
                    stateLayout.showFailView();
                }
            }

            @Override
            public void requestdataFailed(String msg, Throwable e) {
                stateLayout.showFailView();
            }

            @Override
            public void requestComplete() {

            }
        }, getActivity());




    }



    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        pager = (ViewPager) rootView.findViewById(R.id.pager);
        magicIndicator = (MagicIndicator) rootView.findViewById(R.id.magic_indicator);
        stateLayout.showContentView();
        baseFragments = new ArrayList<>();//所有分类界面的fragment
        tabFragmentAdapter = new TabFragmentAdapter(childFragmentManager, baseFragments);
        pager.setAdapter(tabFragmentAdapter);
        initMagicIndicator();

    }


    private void initMagicIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setEnablePivotScroll(false);
        commonNavigator.setFollowTouch(false);
        commonNavigatorAdapter = new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return baseFragments.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setNormalColor(Color.parseColor("#666666"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#333333"));
                simplePagerTitleView.setText(baseFragments.get(index).getTitle());


                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setLineHeight(getResources().getDimension(R.dimen.y4));
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
//    控制下划线之间的间距        linePagerIndicator.setYOffset(getResources().getDimension(R.dimen.y1));
                linePagerIndicator.setColors(Color.parseColor("#FF6248"));

                return linePagerIndicator;
            }
        };

        commonNavigator.setAdapter(commonNavigatorAdapter);
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, pager);
    }
}
